package com.css.zfzx.sjcj.modules.drill.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.css.bpm.platform.api.local.PlatformAPI;
import com.css.bpm.platform.sys.dict.repository.entity.DictItemEntity;
import com.css.bpm.platform.utils.*;
import com.css.zfzx.sjcj.common.utils.CheckObjFields;
import com.css.zfzx.sjcj.common.utils.LocationUtil;
import com.css.zfzx.sjcj.common.utils.ServerUtil;
import com.css.zfzx.sjcj.modules.drill.Entity.QhY02Entity;
import com.css.zfzx.sjcj.modules.drill.Entity.QhY04Entity;
import com.css.zfzx.sjcj.modules.drill.Entity.vo.QhY04Vo;
import com.css.zfzx.sjcj.modules.drill.repository.QhY02Repository;
import com.css.zfzx.sjcj.modules.drill.repository.QhY04NativeRepository;
import com.css.zfzx.sjcj.modules.drill.repository.QhY04Repository;
import com.css.zfzx.sjcj.modules.drill.service.QhService;
import com.css.zfzx.sjcj.modules.drill.service.QhY04Service;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.InvocationTargetException;
import java.util.*;

/**
 * @author Li Helei
 * @date 2021/1/16 10:44
 */
@Slf4j
@Service
public class QhY04ServiceImpl implements QhY04Service {

	@Autowired
    private QhY04Repository qhY04Repository;
    @Autowired
    private QhY04NativeRepository qhY04NativeRepository;
    @Autowired
    private QhService qhService;

    /*@Override
    public JSONObject queryQhY04s(QhY04QueryParams queryParams) {
        List<QhY04Entity> qhY04List = this.qhY04NativeRepository.listQhY04s(queryParams);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("data", qhY04List);
        return jsonObject;
    }*/
    @Override
    public JSONObject queryQhY04s(String drillId, int curPage, int pageSize,String sort,String order) {
        if (!PlatformObjectUtils.isEmpty(sort) && sort.length() > 0){
            sort= CheckObjFields.propertyChange(sort);
        }
        Page<QhY04Entity> qhY04Page = this.qhY04NativeRepository.queryQhY04s(drillId, curPage, pageSize, sort, order);
        JSONObject jsonObject = PlatformPageUtils.formatPageData(qhY04Page);
        return jsonObject;
    }

    /*
     * @Description: 根据外键drillId查询实体集合
     * @Author:      a
     * @Param:
     * @Return
      **/
    @Override
    public List<QhY04Entity> findByDrillId(String drillId){
        return this.qhY04Repository.findByIsValidAndDrillIdOrderByAssiseNumber("1", drillId);
    };

    @Override
    public QhY04Entity getQhY04(String id) {
        QhY04Entity qhY04 = this.qhY04Repository.findByIdAndIsValid(id,"1");
         return qhY04;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public QhY04Entity saveQhY04(QhY04Entity qhY04) {
        String uuid = UUIDGenerator.getUUID();
        qhY04.setId(uuid);
        qhY04.setIsValid("1");
        this.qhY04Repository.save(qhY04);
        try {
            qhService.update(qhY04);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return qhY04;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public QhY04Entity updateQhY04(QhY04Entity qhY04) {
        this.qhY04Repository.save(qhY04);
        try {
            qhService.update(qhY04);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return qhY04;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteQhY04s(String ids) {
        List<String> idList = Arrays.asList(ids.split(","));
        List<QhY04Entity> qhY04List = this.qhY04Repository.findAllById(idList);
        if(!PlatformObjectUtils.isEmpty(qhY04List) && qhY04List.size() > 0) {
            for(QhY04Entity qhY04 : qhY04List) {
                qhY04.setIsValid("0");
                this.qhY04Repository.save(qhY04);
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void submitQhY04(String ids) {
        List<String> idList = Arrays.asList(ids.split(","));
        List<QhY04Entity> qhY04List = this.qhY04Repository.findAllById(idList);
        if(!PlatformObjectUtils.isEmpty(qhY04List) && qhY04List.size() > 0) {
            for(QhY04Entity qhY04 : qhY04List) {
                this.qhY04Repository.save(qhY04);
            }
        }
    }

    @Override
    public List<QhY04Vo> dataQualityCheck(String drillId){
        JSONObject res = new JSONObject();
        List<QhY04Vo> qhY04VoList=new ArrayList<>();
        try {
            List<QhY04Entity> qhY04List=qhY04Repository.findByIsValidAndDrillIdOrderByAssiseNumber("1", drillId);
            if(!PlatformObjectUtils.isEmpty(qhY04List) && qhY04List.size() > 0) {
                for (QhY04Entity qhY04 : qhY04List) {
                    QhY04Vo qhY04Vo=new QhY04Vo();
                    qhY04Vo.setId(qhY04.getId());
                    //检查字段值不为空
                    StringBuilder errorMsg = new StringBuilder();
                    Map<String, String> methods = new HashMap<>();
                    setCheckFields(methods);

                    ServerUtil.checkFieldNull(qhY04, methods, errorMsg);
                    if(errorMsg.length()>0|| StringUtils.isNotEmpty(errorMsg.toString())) {
                        qhY04Vo.setQualityOpinion("Y04的"+errorMsg.toString());
                        qhY04Vo.setDataState("1");
                        qhY04VoList.add(qhY04Vo);
                        break;
                    }
                    //检查字段属性
                    String assiseCode=qhY04.getAssiseCode();
                    String formationAge=qhY04.getFormationAge();
                    String causeAge=qhY04.getCauseAge();
                    Double bottomDepth=qhY04.getBottomDepth();
                    Double layerThickness=qhY04.getLayerThickness();
                    String geotechnicalName=qhY04.getGeotechnicalName();
                    String geotechnicalCharacteristics=qhY04.getGeotechnicalCharacteristics();
                    if (StringUtils.isNotEmpty(assiseCode)){
                        if (assiseCode.length()>20){
                            ServerUtil.addErrorMsg(errorMsg, "地层编号", "不多于20个字符");
                        }
                    }
                    if (formationAge.length()>20){
                        ServerUtil.addErrorMsg(errorMsg, "地层年代", "不多于20个字符");
                    }
                    if (StringUtils.isNotEmpty(causeAge)){
                        if (causeAge.length()>20){
                            ServerUtil.addErrorMsg(errorMsg, "时代成因", "不多于20个字符");
                        }
                    }
                    if ((bottomDepth+"").length()-(bottomDepth+"").indexOf(".")-1>2){
                        ServerUtil.addErrorMsg(errorMsg, "层底深度", "小数点后最多两位");
                    }
                    if ((layerThickness+"").length()-(layerThickness+"").indexOf(".")-1>2){
                        ServerUtil.addErrorMsg(errorMsg, "分层厚度", "小数点后最多两位");
                    }
                    if (geotechnicalName.length()>20){
                        ServerUtil.addErrorMsg(errorMsg, "岩土名称", "不多于20个字符");
                    }
                    if (geotechnicalCharacteristics.length()>1000){
                        ServerUtil.addErrorMsg(errorMsg, "岩土特征", "不多于1000个汉字字符");
                    }

                    String strErrorMsg = errorMsg.toString();
                    if(!strErrorMsg.isEmpty()) {
                        qhY04Vo.setQualityOpinion("Y04的"+strErrorMsg);
                        qhY04Vo.setDataState("1");
                        break;
                    } else {
                        qhY04Vo.setQualityOpinion("系统自动质检通过");
                        qhY04Vo.setDataState("2");
                    }
                    qhY04VoList.add(qhY04Vo);
                }

            }
        } catch (Exception e){
            log.error(e.getMessage());
        }
        return qhY04VoList;
    }
    private void setCheckFields(Map<String,String> methods){
        methods.put("assiseNumber", "地层序号");
        //methods.put("assiseCode", "地层编号");
        methods.put("formationAge", "地层年代");
        //methods.put("causeAge", "时代成因");
        methods.put("bottomDepth", "层底深度");
        methods.put("layerThickness", "分层厚度");
        methods.put("geotechnicalName", "岩土名称");
        methods.put("geotechnicalCharacteristics", "岩土特征");
    }
}
